package com.ruoyi.qiushi.service.equipment.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.qiushi.domain.followup.QiushiFollowRecord;
import com.ruoyi.qiushi.domain.system.QiushiMessage;
import com.ruoyi.qiushi.domain.user.MemberFamily;
import com.ruoyi.qiushi.domain.user.MemberUserFamily;
import com.ruoyi.qiushi.mapper.followup.QiushiFollowRecordMapper;
import com.ruoyi.qiushi.mapper.system.QiushiMessageMapper;
import com.ruoyi.qiushi.mapper.user.MemberUserFamilyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.qiushi.mapper.equipment.QiushiWarningMapper;
import com.ruoyi.qiushi.domain.equipment.QiushiWarning;
import com.ruoyi.qiushi.service.equipment.IQiushiWarningService;

/**
 * 预警Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-10
 */
@Service
public class QiushiWarningServiceImpl implements IQiushiWarningService {
    @Autowired
    private QiushiWarningMapper qiushiWarningMapper;
    @Autowired
    private QiushiMessageMapper qiushiMessageMapper;
    @Autowired
    private MemberUserFamilyMapper memberUserFamilyMapper;
    @Autowired
    private QiushiFollowRecordMapper qiushiFollowRecordMapper;

    public int handleSend(QiushiWarning qiushiWarning) {
        System.out.println();
        System.out.println("tezt:\t" + qiushiWarning);
        System.out.println();

        QiushiMessage qiushiMessage = new QiushiMessage();
        qiushiWarning.setCreateTime(new Date());
        qiushiWarning.setUpdateTime(new Date());
        qiushiWarning.setFollowStatus("1");

        MemberUserFamily memberUserFamily = new MemberUserFamily();
        memberUserFamily.setFamilyId(qiushiWarning.getFamilyId());
        List<MemberUserFamily> memberUserFamilyList = memberUserFamilyMapper.selectMemberUserFamilyList(memberUserFamily);

        if (memberUserFamilyList.size() == 1) {
            qiushiMessage.setUserId(memberUserFamilyList.get(0).getUserId());
        } else {
            boolean flag = false;
            for (MemberUserFamily temp : memberUserFamilyList) {
                if (temp.getRelation().equals("0")) {
                    qiushiMessage.setUserId(temp.getUserId());
                    flag = true;
                    break;
                }
            }
            if (!flag) qiushiMessage.setUserId(memberUserFamilyList.get(0).getUserId());
        }

        qiushiMessage.setCreateTime(new Date());
        qiushiMessage.setUpdateTime(new Date());
        qiushiMessage.setTitle(qiushiWarning.getFormName());
        qiushiMessage.setContent(qiushiWarning.getFormName());
        qiushiMessage.setJoinId(qiushiWarning.getFormId());
        qiushiMessage.setType("2");

        qiushiMessageMapper.insertQiushiMessage(qiushiMessage);

        QiushiFollowRecord qiushiFollowRecord = new QiushiFollowRecord();
        qiushiFollowRecord.setCreateTime(new Date());
        qiushiFollowRecord.setUpdateTime(new Date());
        qiushiFollowRecord.setFamilyId(qiushiWarning.getFamilyId());
        qiushiFollowRecord.setJoinId(qiushiWarning.getFormId());
        qiushiFollowRecord.setType("2");

        qiushiFollowRecordMapper.insertQiushiFollowRecord(qiushiFollowRecord);

        return qiushiWarningMapper.updateQiushiWarning(qiushiWarning);
    }

    /**
     * 查询预警
     *
     * @param id 预警主键
     * @return 预警
     */
    @Override
    public QiushiWarning selectQiushiWarningById(Long id) {
        return qiushiWarningMapper.selectQiushiWarningById(id);
    }

    /**
     * 查询预警列表
     *
     * @param qiushiWarning 预警
     * @return 预警
     */
    @Override
    public List<QiushiWarning> selectQiushiWarningList(QiushiWarning qiushiWarning) {
        return qiushiWarningMapper.selectQiushiWarningList(qiushiWarning);
    }

    /**
     * 新增预警
     *
     * @param qiushiWarning 预警
     * @return 结果
     */
    @Override
    public int insertQiushiWarning(QiushiWarning qiushiWarning) {
        qiushiWarning.setCreateTime(DateUtils.getNowDate());
        qiushiWarning.setUpdateTime(DateUtils.getNowDate());
        return qiushiWarningMapper.insertQiushiWarning(qiushiWarning);
    }

    /**
     * 修改预警
     *
     * @param qiushiWarning 预警
     * @return 结果
     */
    @Override
    public int updateQiushiWarning(QiushiWarning qiushiWarning) {
        qiushiWarning.setUpdateTime(DateUtils.getNowDate());

        qiushiWarning.setAuditTime(new Date());
        qiushiWarning.setAuditUserId(SecurityUtils.getUserId());
        qiushiWarning.setAuditUserName(SecurityUtils.getUsername());

        return qiushiWarningMapper.updateQiushiWarning(qiushiWarning);
    }

    /**
     * 批量删除预警
     *
     * @param ids 需要删除的预警主键
     * @return 结果
     */
    @Override
    public int deleteQiushiWarningByIds(Long[] ids) {
        return qiushiWarningMapper.deleteQiushiWarningByIds(ids);
    }

    @Override
    public int warningCount(Long familyId, Long deviceId) {
        return qiushiWarningMapper.warningCount(familyId,deviceId);
    }

    /**
     * 删除预警信息
     *
     * @param id 预警主键
     * @return 结果
     */
    @Override
    public int deleteQiushiWarningById(Long id) {
        return qiushiWarningMapper.deleteQiushiWarningById(id);
    }
}
